Option Explicit On 

Imports System.IO

Public Class Logger

   ' Read-only
   Public Shared LOGLEVEL_ERROR As Integer = 3
   Public Shared LOGLEVEL_WARN As Integer = 2
   Public Shared LOGLEVEL_INFO As Integer = 1
   Public Shared LOGLEVEL_DEBUG As Integer = 0

   ' Read/Write
   Dim InfoLogFile As String
   Dim ErrorLogFile As String
   Dim LogLevel As Integer
   Dim m_WriteToConsole As Boolean = False


   Public Sub New()

      ' Logfiles
      InfoLogFile = "info.log"
      ErrorLogFile = "error.log"

      ' Settings
      LogLevel = LOGLEVEL_ERROR

   End Sub

   Public Sub New(ByVal InfoFile As String, ByVal ErrorFile As String, ByVal ErrLevel As String)

      ' Logfiles
      InfoLogFile = InfoFile
      ErrorLogFile = ErrorFile

      ' Settings
      Select Case LCase(Trim(ErrLevel))
         Case "debug" : LogLevel = LOGLEVEL_DEBUG
         Case "info" : LogLevel = LOGLEVEL_INFO
         Case "warn" : LogLevel = LOGLEVEL_WARN
         Case "error" : LogLevel = LOGLEVEL_ERROR
      End Select

   End Sub


   '''''''''''''''''''''''''''''''''''''
   ' Public Methods
   '''''''''''''''''''''''''''''''''''''

   Public Sub dpa(ByVal data)

      ' Log to console
      If m_WriteToConsole Then Console.WriteLine("MESSAGE: " & data)
      ' Log to log file
      If InfoLogFile <> "" Then AppendToLogFile(InfoLogFile, Now() & vbTab & "MESSAGE: " & data & vbNewLine)

   End Sub

   Public Sub dpe(ByVal data)

      If LogLevel > LOGLEVEL_ERROR Then Exit Sub
      Dim ErrDescription
      ErrDescription = Err.Description

      ' Log to console
      If m_WriteToConsole Then Console.WriteLine("ERROR: " & data & " : " & ErrDescription)
      ' Log to log file
      If InfoLogFile <> "" Then AppendToLogFile(ErrorLogFile, Now() & vbTab & "ERROR: " & vbTab & data & vbTab & Err.Number & ":" & ErrDescription & vbNewLine)

   End Sub

   Public Sub dpw(ByVal data)

      If LogLevel > LOGLEVEL_WARN Then Exit Sub

      ' Log to console
      If m_WriteToConsole Then Console.WriteLine("WARNING: " & data)
      ' Log to log file
      If InfoLogFile <> "" Then AppendToLogFile(InfoLogFile, Now() & vbTab & "WARNING: " & data & vbNewLine)

   End Sub

   Public Sub dpi(ByVal data)

      If LogLevel > LOGLEVEL_INFO Then Exit Sub

      ' Log to console
      If m_WriteToConsole Then Console.WriteLine("INFO: " & data)
      ' Log to log file
      If InfoLogFile <> "" Then AppendToLogFile(InfoLogFile, Now() & vbTab & "INFO: " & data & vbNewLine)

   End Sub

   Public Sub dpd(ByVal data)

      If LogLevel > LOGLEVEL_DEBUG Then Exit Sub

      ' Log to console
      If m_WriteToConsole Then Console.WriteLine("DEBUG: " & data)
      ' Log to log file
      If InfoLogFile <> "" Then AppendToLogFile(InfoLogFile, Now() & vbTab & "DEBUG: " & data & vbNewLine)

   End Sub

   Public Sub Clear()

      ' Save empty string to log files
      File.Delete(InfoLogFile)
      File.Delete(ErrorLogFile)

   End Sub

   Public Function Verify() As Boolean
      ' Verify that the logs can be written

      Try
         ' Append empty string to both log files
         AppendToLogFile(InfoLogFile, "")
         AppendToLogFile(ErrorLogFile, "")
         Return True
      Catch e As IOException
         Return False
      End Try

   End Function


   '''''''''''''''''''''''''''''''''''''
   ' Private Methods
   '''''''''''''''''''''''''''''''''''''

   Sub AppendToLogFile(ByVal filename As String, ByVal data As String)
      ' Append data to filename, will create file If it doesn't exist

      Dim Writer As StreamWriter = File.AppendText(filename)
      Writer.Write(data)
      Writer.Close()

   End Sub

   Sub SaveToLogFile(ByVal filename As String, ByVal data As String)
      ' Save data to filename

      Dim Writer As StreamWriter = New StreamWriter(filename)
      Writer.Write(data)
      Writer.Close()

   End Sub

   '''''''''''''''''''''''''''''''''''''
   ' Properties
   '''''''''''''''''''''''''''''''''''''
   Public Property WriteToConsole() As Boolean

      Get
         Return m_WriteToConsole
      End Get

      Set(ByVal Value As Boolean)
         m_WriteToConsole = Value
      End Set

   End Property



End Class
